package com.sunny.myproject.storem.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sunny.myproject.core.base.action.BaseAction;
import com.sunny.myproject.storem.entity.Materiel;
import com.sunny.myproject.storem.entity.Warehouse;
import com.sunny.myproject.storem.service.MaterielService;
import com.sunny.myproject.storem.service.WarehouseService;

@Controller
@RequestMapping("/storem/warehouse")
public class WarehouseAction extends BaseAction<Warehouse>{

	private String title = "仓库";
	private String action = "storem/warehouse";
	private String function = "warehouse";
	
	private Logger logger = Logger.getLogger(WarehouseAction.class);
	
	@Autowired
	private WarehouseService service;
	@Autowired
	private MaterielService materielService;
	
	@Override
	@RequestMapping(params="list")
	public String list(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			super.pager = super.pageStart(model, request, response, action, function, title);
			super.pager = super.pageEnd(model, service.num(criterions), pager);
			super.returnValue(model, service.find(criterions, pager));
		} catch (Exception e) {
			logger.error(e);
		}
		return "storem/warehouseList";
	}

	@Override
	@RequestMapping(params="add")
	public String add(HttpServletRequest request, HttpServletResponse response) {
		super.add(title, request);
		return "storem/warehouseEdit";
	}

	@Override
	@RequestMapping(params="edit")
	public String edit(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			super.returnValue(model, service.findById(super.edit(title, model, request)));
		} catch (Exception e) {
			logger.error(e);
		}
		return "storem/warehouseEdit";
	}

	@Override
	@RequestMapping(params="delete")
	public String delete(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			service.deleteByIds(super.param2(request));
		} catch (Exception e) {
			logger.error(e);
		}
		return "redirect:/storem/warehouse?list";
	}

	@Override
	@RequestMapping(params="save")
	public String save(HttpServletRequest request,
			HttpServletResponse response, Warehouse t) {
		try {
			service.save(t);
		} catch (Exception e) {
			logger.error(e);
		}
		return "redirect:/storem/warehouse?list";
	}

	@Override
	@RequestMapping(params="view")
	public String view(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			super.returnViewValue(model, service.findById(super.view(title, model, request)));
		} catch (Exception e) {
			logger.error(e);
		}
		return "storem/warehouseEdit";
	}
	
	@RequestMapping(params="addMateriel")
	public String addMateriel(Model model, HttpServletRequest request,
			HttpServletResponse response){
		super.returnValue_title(model, "添加物料");
		return "storem/warehouseAddMateriel";
	}
	
	@RequestMapping(params="saveMateriel")
	public String saveMateriel(Model model,HttpServletRequest request,
			HttpServletResponse response,@ModelAttribute Materiel materiel){
		//保存materiel
		materielService.save(materiel);
		
		//保存warehouse
		Warehouse warehouse = new Warehouse();
		warehouse.setMateriel(materiel);
		warehouse.setNowStock(0);
		service.save(warehouse);
		
		return "redirect:/storem/warehouse?addMateriel";
	}
	
	@RequestMapping(params="queryMateriel")
	public String queryMateriel(Model model,HttpServletRequest request,
			HttpServletResponse response){
		try {
			super.pager = super.pageStart(model, request);
			super.pager = super.pageEnd(model, materielService.num(criterions), pager);
			super.returnValue(model, materielService.find(criterions, pager));
		} catch (Exception e) {
			logger.error(e);
		}
		return "storem/warehouseQueryMateriel";
	}
}
